<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="客户名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入客户全称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入会员名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员手机" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTimes" label-width="100">
        <el-date-picker
          v-model="queryParams.createTimes"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="会员类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="(item, index) in user_type"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="会员状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="会员状态"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in company_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleUserAdd"
          v-hasPermi="['company:companyUser:add']"
          >添加客户</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" align="center" prop="companyCode"  width="110" />
      <el-table-column label="客户全称" align="center" prop="fullName" min-width="150"/>
      <el-table-column label="客户简称" align="center" prop="companyName" width="110" />
      <el-table-column label="会员名称" align="center" prop="name" />
      <el-table-column label="登录账号" align="center" prop="account" width="80" />
      <el-table-column label="会员手机号" align="center" prop="mobile" width="110"/>
      <el-table-column label="会员类型" align="center" prop="type" width="100" >
        <template #default="scope">
          <dict-tag :options="user_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="会员状态" align="center" prop="status" width="80" >
        <template #default="scope">
          <dict-tag :options="company_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" width="100"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="160"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="300"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            >详细</el-button
          >
          <el-button
            link
            type="primary"
            v-if="scope.row.status === 'NORMAL'"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDisable(scope.row)"
            >停用</el-button
          >
          <el-button
            link
            type="primary"
            v-if="scope.row.status === 'NORMAL'"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      v-bind="$attrs"
      v-model="open"
      title="添加会员信息"
      append-to-body
      width="600px"
    >
      <el-form ref="userRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="客户信息" prop="fullName">
              <el-select
                v-model="form.fullName"
                placeholder=""
                filterable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="item in companyList"
                  :key="item.fullName"
                  :label="item.fullName"
                  :value="item.fullName"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员名字" prop="name">
              <el-input
                v-model="form.name"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员手机号" prop="mobile">
              <el-input
                v-model="form.mobile"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="account">
              <el-input
                v-model="form.account"
                placeholder=""
                readonly
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录密码" prop="password">
              <el-input
                v-model="form.password"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员状态" prop="status">
              <el-select
                v-model="form.status"
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in company_status"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员类型" prop="type">
              <el-select
                v-model="form.type"
                placeholder=""
                clearable
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in user_type"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" v-if="isDetail" @click="submitForm"
          >确定</el-button
        >
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import {
  listUser,
  getUser,
  addUser,
  updateUser,
  delUser,
  disableUser,
  listCompany,
} from "@/api/company/user.js";

import { formatDate } from "@/utils/date.js";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const { company_status } = proxy.useDict("company_status");
const { company_audit_status } = proxy.useDict("company_audit_status");
const { user_type } = proxy.useDict("user_type");

const userList = ref([]);
const companyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const isDetail = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    companyName: undefined,
    status: null,
    type: undefined,
    mobile: undefined,
    account: undefined,
    fullName: undefined,
    companyCode: undefined,
    name: undefined,
    createTimes: ["", ""],
    startTime: "",
    endTIme: "",
  },
  rules: {
    fullName: [
      { required: true, message: "客户信息不能为空", trigger: "blur" },
    ],
    name: [{ required: true, message: "会员名字不能为空", trigger: "blur" }],
    mobile: [
      { required: true, message: "会员手机号不能为空", trigger: "blur" },
    ],
    password: [
      { required: true, message: "登录密码不能为空", trigger: "blur" },
    ],
    status: [{ required: true, message: "会员状态不能为空", trigger: "blur" }],
    type: [{ required: true, message: "会员类型不能为空", trigger: "blur" }],
  },
  paymentTypeOptions: [
    {
      label: "实时付款",
      value: 1,
    },
    {
      label: "信用付款",
      value: 2,
    },
  ],
  companyStatusOptions: [
    {
      label: "正常",
      value: "NORMAL",
    },
    {
      label: "停用",
      value: "CANCEL",
    },
  ],
  genreIdOptions: [
    {
      label: "视光版",
      value: 7,
    },
    {
      label: "医疗版",
      value: 8,
    },
  ],
});

const {
  queryParams,
  form,
  rules,
  paymentTypeOptions,
  companyStatusOptions,
  genreIdOptions,
} = toRefs(data);

function getList() {
  loading.value = true;
  queryParams.value.startTime = null;
  queryParams.value.endTime = null;
  if (queryParams.value.createTimes) {
    queryParams.value.startTime =
      queryParams.value.createTimes[0] === ""
        ? queryParams.value.createTimes[0]
        : formatDate(queryParams.value.createTimes[0], "yyyy-MM-dd hh:mm:ss");
    queryParams.value.endTime =
      queryParams.value.createTimes[1] === ""
        ? queryParams.value.createTimes[1]
        : formatDate(queryParams.value.createTimes[1], "yyyy-MM-dd hh:mm:ss");
  }
  listUser(queryParams.value).then((response) => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleCompanyQuery(query) {
  listCompany(query).then((response) => {
    companyList.value = response.data;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
  isDetail.value = true;
}
/** 表单重置 */
function reset() {
  form.value = {
    userId: undefined,
    companyName: undefined,
    companyId: undefined,
    status: null,
    type: undefined,
    mobile: undefined,
    account: undefined,
    fullName: undefined,
    companyCode: undefined,
    name: undefined,
    createTimes: ["", ""],
    startTime: "",
    endTIme: "",
  };
  proxy.resetForm("userRef");
  handleCompanyQuery();
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleUserAdd() {
  reset();
  open.value = true;
  form.value.status = "NORMAL";
  form.value.type = "USER";
  isDetail.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  isDetail.value = true;
  reset();
  form.value = row;
  open.value = true;
  // const postId = row.userId;
  // getUser(postId).then(response => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改岗位";
  // });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["userRef"].validate((valid) => {
    if (valid) {
      if (form.value.userId != undefined) {
        updateUser(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUser(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleView(row) {
  open.value = true;
  form.value = row;
  isDetail.value = false;
}

/** 停用按钮操作 */
function handleDisable(row) {
  const userId = row.userId;
  proxy.$modal
    .confirm("是否停用此账号信息")
    .then(function () {
      return disableUser(userId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("操作成功");
    })
    .catch(() => {});
}
/** 删除按钮操作 */
function handleDelete(row) {
  const userId = row.userId;
  proxy.$modal
    .confirm("是否删除此账号信息")
    .then(function () {
      return delUser(userId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("操作成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */

getList();
</script>
